home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex13-4.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  4KB  |  191 lines

  1. // ex13-4.c -- Class AmphibVhcl with virtual base class
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex13-4.c,v 3.0 90/05/15 22:44:41 kgorlen Rel $
  4.  
  5. #include <iostream.h>
  6.  
  7. class Link;
  8.  
  9. class LinkedList {
  10.     Link* firstLink;    // pointer to first Link of list 
  11.     Link* lastLink;     // pointer to last Link of list 
  12. public:
  13.     LinkedList()        { firstLink = lastLink = 0; }
  14.     Link* add(Link& l);
  15.     virtual void printOn(ostream& strm =cout) const;
  16. };
  17.  
  18. class Link {
  19.     Link* next;
  20.     friend LinkedList;
  21. public:
  22.     Link()                  { next = 0; }
  23.     Link* nextLink() const  { return next; }
  24.     virtual void printOn(ostream& strm =cout) const =0;
  25. };
  26.  
  27. Link* LinkedList::add(Link& l)
  28. {
  29.     if (firstLink == 0) firstLink = lastLink = &l;
  30.     else {
  31.         lastLink->next = &l;
  32.         lastLink = &l;
  33.     }
  34.     return &l;
  35. }
  36.  
  37. void LinkedList::printOn(ostream& strm) const
  38. {
  39.     int n = 0;
  40.     Link* l = firstLink;
  41.     while (l != 0) {
  42.         if (n++ != 0) strm << endl;
  43.         l->printOn(strm);
  44.         l = l->next;
  45.     }
  46. }
  47.  
  48. //--------------------------------------------
  49.  
  50. class AllLink: public Link {
  51. protected:
  52.     AllLink() {};
  53.     virtual void printOn(ostream& strm =cout) const =0;
  54. };
  55.  
  56. class QLink: public Link {
  57. protected:
  58.     QLink() {};
  59.     virtual void printOn(ostream& strm =cout) const =0;
  60. };
  61.  
  62. class AllVehicles: public LinkedList {
  63. public:
  64.     virtual void addVehicle(AllLink&);
  65. };
  66.  
  67. void AllVehicles::addVehicle(AllLink& l)    { add(l); }
  68.  
  69. class VehicleQ: public LinkedList {
  70. public:
  71.     virtual void addVehicle(QLink&);
  72. };
  73.  
  74. void VehicleQ::addVehicle(QLink& l)     { add(l); }
  75.  
  76. class Vehicle: public AllLink, public QLink {
  77.     static unsigned vehicleID;
  78.     static AllVehicles allVehicles;
  79.     unsigned id;
  80.     float height;
  81.     float length;
  82. protected:
  83.     Vehicle(float h = 0.0, float l = 0.0) {
  84.         id = ++vehicleID;
  85.         height = h; length = l;
  86.         allVehicles.addVehicle(*this);
  87.     }
  88. public:
  89.     static void printAll(ostream& strm =cout) {
  90.         allVehicles.printOn(strm);
  91.     }
  92.     virtual void printOn(ostream& strm =cout) const;
  93. // ...
  94. };
  95.  
  96. unsigned Vehicle::vehicleID = 0;
  97. AllVehicles Vehicle::allVehicles;
  98.  
  99. void Vehicle::printOn(ostream& strm) const
  100. {
  101.     strm << '#' << id << " height " << height << "  length "
  102.         << length;
  103. }
  104.  
  105. class LandVhcl: public virtual Vehicle {
  106.     unsigned axles;
  107. public:
  108.     LandVhcl(float h, float l, unsigned a =2) : Vehicle(h,l)
  109.         { axles = a; }
  110.     virtual void printOn(ostream& strm =cout) const;
  111. // ...
  112. };
  113.  
  114. void LandVhcl::printOn(ostream& strm) const
  115. {
  116.     Vehicle::printOn(strm);
  117.     strm << "  axles " << axles;
  118. }
  119.  
  120. class WaterVhcl: public virtual Vehicle {
  121.     float draft;
  122. public:
  123.     WaterVhcl(float h, float l, float d) : Vehicle(h,l)
  124.         { draft = d; }
  125.     virtual void printOn(ostream& strm =cout) const;
  126. // ...
  127. };
  128.  
  129. void WaterVhcl::printOn(ostream& strm) const
  130. {
  131.     Vehicle::printOn(strm);
  132.     strm << "  draft " << draft;
  133. }
  134.  
  135. class AmphibVhcl: public LandVhcl, public WaterVhcl {
  136. public:
  137.     AmphibVhcl(float h, float l, float d, unsigned a=2)
  138.         : Vehicle(h,l), LandVhcl(h,l,a), WaterVhcl(h,l,d) {}
  139.     virtual void printOn(ostream& strm =cout) const;
  140. // ...
  141. };
  142.  
  143. void AmphibVhcl::printOn(ostream& strm) const
  144. {
  145.     LandVhcl::printOn(strm);
  146.     WaterVhcl::printOn(strm);
  147. }
  148.  
  149. class StopLightQ: public VehicleQ {
  150. public:
  151.     virtual void addVehicle(LandVhcl&);
  152. };
  153.  
  154. void StopLightQ::addVehicle(LandVhcl& v)
  155. {
  156.     VehicleQ::addVehicle(v);
  157. }
  158.  
  159. class DrawBridgeQ: public VehicleQ {
  160. public:
  161.     virtual void addVehicle(WaterVhcl&);
  162. };
  163.  
  164. void DrawBridgeQ::addVehicle(WaterVhcl& v)
  165. {
  166.     VehicleQ::addVehicle(v);
  167. }
  168.  
  169. StopLightQ stopLightQ[2];
  170. DrawBridgeQ drawBridgeQ;
  171.  
  172. main()
  173. {
  174.     stopLightQ[0].addVehicle(*new LandVhcl(4.1, 12.0));
  175.     stopLightQ[1].addVehicle(*new LandVhcl(4.2, 12.0));
  176.     stopLightQ[0].addVehicle(*new LandVhcl(4.3, 12.0));
  177.     stopLightQ[1].addVehicle(*new LandVhcl(4.4, 12.0));
  178.     drawBridgeQ.addVehicle(*new WaterVhcl(21.0, 19.0, 3.5));
  179.     drawBridgeQ.addVehicle(*new WaterVhcl(10.0, 30.0, 2.0));
  180.     stopLightQ[0].addVehicle(*new AmphibVhcl(5.0, 15.0, 3.0));
  181.     drawBridgeQ.addVehicle(*new AmphibVhcl(5.1, 15.0, 3.0));
  182.     cout << "allVehicles:\n"; Vehicle::printAll();
  183.     cout << endl;
  184.     cout << "stopLightQ[0]:\n"; stopLightQ[0].printOn();
  185.     cout << endl;
  186.     cout << "stopLightQ[1]:\n"; stopLightQ[1].printOn();
  187.     cout << endl;
  188.     cout << "drawBridgeQ:\n"; drawBridgeQ.printOn();
  189.     cout << endl;
  190. }
  191.